//
//  LJVistorView.swift
//  LJWeibo
//
//  Created by Raymond on 15/12/15.
//  Copyright © 2015年 liu. All rights reserved.
//

import UIKit
protocol LJVistorViewDelegate: NSObjectProtocol{
    /// 注册按钮被点击时调用
    func vistorViewDidClickRegisterButton()
    /// 登录按钮被点击时调用
    func vistorViewDidClickLoginButton()
    
}
class LJVistorView: UIView {
    
    weak var delegate: LJVistorViewDelegate?

    override init(frame: CGRect) {
        
        super.init(frame: frame)
        //设置背景颜色
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /**
    添加自控的方法
    */
    private func prepareUI() {
        //添加子控件
        self.addSubview(iconView)
        self.addSubview(coverView)
        self.addSubview(homeView)
        self.addSubview(messageLabel)
        self.addSubview(registerButton)
        self.addSubview(loginButton)
        
        
        //添加约束
        iconView.ff_AlignInner(type: ff_AlignType.CenterCenter, referView: self, size: nil, offset: CGPointMake(0, -40))
        
        //房子
        homeView.ff_AlignInner(type: ff_AlignType.CenterCenter, referView: iconView, size: nil)

        //文字
        messageLabel.ff_AlignVertical(type: ff_AlignType.BottomCenter, referView: iconView, size: nil, offset: CGPointMake(0, 16))
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 224))

        //注册按钮
        registerButton.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: messageLabel, size: CGSize(width: 100, height: 35), offset: CGPoint(x: 0, y: 16))

        
        //登录按钮
        loginButton.ff_AlignVertical(type: ff_AlignType.BottomRight, referView: messageLabel, size: CGSize(width: 100, height: 35), offset: CGPoint(x: 0, y: 16))
        
        //遮罩视图
        coverView.ff_AlignInner(type: ff_AlignType.CenterCenter, referView: iconView, size: nil)

        
    }
    
    func setupInfo(imageName: String, message: String){
        // 设置图片
        iconView.image = UIImage(named: imageName)
        //设置文字
        messageLabel.text = message
        messageLabel.sizeToFit()
        //隐藏小房子
        homeView.hidden = true
        //将遮罩层移动到底部
        sendSubviewToBack(coverView)
    }
    
 /// 开启转轮动画
    func startAnimation(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        //完成时不移除动画
        anim.removedOnCompletion = false
        
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    /// 暂停旋转
    func pauseAnimation() {
        // 记录暂停时间
        let pauseTime = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        
        // 设置动画速度为0
        iconView.layer.speed = 0
        
        // 设置动画偏移时间
        iconView.layer.timeOffset = pauseTime
    }
    
    /// 恢复旋转
    func resumeAnimation() {
        // 获取暂停时间
        let pauseTime = iconView.layer.timeOffset
        
        // 设置动画速度为1
        iconView.layer.speed = 1
        
        // 重置偏移时间
        iconView.layer.timeOffset = 0
        
        // 重置开始时间
        iconView.layer.beginTime = 0
        
        // 计算开始时间
        let timeSincePause = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
        
        // 设置开始时间
        iconView.layer.beginTime = timeSincePause
    }
    //MARK: - 按钮监听方法
    func loginButtonDidClick(){
        delegate?.vistorViewDidClickLoginButton()
    }
    func registerButtonDidClick(){
        delegate?.vistorViewDidClickRegisterButton()
    }
    
    //MARK: - 懒加载
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon")) ///< 转轮
    
    private lazy var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house")) ///< 房子
    
    private lazy var messageLabel: UILabel = {
        var label = UILabel()
        label.text = "关注一些人,看看这里有什么惊喜!"
        label.textColor = UIColor.darkGrayColor()
        label.numberOfLines = 0
        return label
    }() ///< 文字
    
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.addTarget(self, action: "registerButtonDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
        
    }()///< 注册按钮
    
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        //添加监听事件
        button.addTarget(self, action: "loginButtonDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
        
    }()///< 登录按钮
    
    private lazy var coverView: UIImageView = {
        var view = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return view
        
    }()
    
    
}
